package com.project.base.baseproject.Activity.LoginActivity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.jaeger.library.StatusBarUtil;
import com.project.base.baseproject.Activity.MainActivity.MainActivity;
import com.project.base.baseproject.AppCustoms.LoadingUpView.LoadingDialog;
import com.project.base.baseproject.AppCustoms.MyApplication.MyApplication;
import com.project.base.baseproject.AppCustoms.Tool.StringUtil;
import com.project.base.baseproject.ModelManager.IdentityManager.IdentityHttp;
import com.project.base.baseproject.ModelManager.Models.User;
import com.project.base.baseproject.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

//登陆
public class LoginActivity extends AppCompatActivity {
    @BindView(R.id.login_account_edit) EditText loginAccount;
    @BindView(R.id.login_password_edit) EditText loginPassword;
    //请求加载指示器
    LoadingDialog loadingDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_layout);
        //设置沉侵式状态栏
        StatusBarUtil.setColor(this, getResources().getColor(R.color.black),0);
        //绑定界面元素
        ButterKnife.bind(this);
        loadingDialog = new LoadingDialog(this);
        //用rxjava显示登陆按钮可点击性
    }
    @OnClick(R.id.login_login_btn)
    public void loginClick() {
        //检查输入合法性
        if(StringUtil.isBlank(loginAccount.getText().toString())) {
            return;
        }
        if(StringUtil.isBlank(loginPassword.getText().toString())) {
            return;
        }
        //进行登陆操作
        loadingDialog.show();
        IdentityHttp.userLogin(loginAccount.getText().toString(),loginPassword.getText().toString(),new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                //这里我们模拟登陆成功了 获取用户信息
                User user = new User();
                user.setId(10001);
                user.setName("TheBloodElf");
                user.setCurrCompanyNo(10001);
                user.setAvatar("http://mobile.59bang.com/content/i/bangbang_log.png");
                user.setUser_guid("0000-1111-2222-3333-4444-5555");
                //写入数据库
                MyApplication.userManager.updateUser(user);
                //修改登陆模型管理器值，记录当前用户giud
                MyApplication.identityManager.identity.user_guid = user.getUser_guid();
                MyApplication.identityManager.saveAuthorizeData();
                loadingDialog.close();
                //进入首页
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        });
    }
}
